package com.goldman.http.common.s2s.model;

import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @Author: Vincent
 * @Date: 2024/1/31 15:56
 */
public class CustomTypeReference<T> extends com.fasterxml.jackson.core.type.TypeReference<T> {
    protected final Type type;

    protected <E> CustomTypeReference(Class<E> cls) {
        Type type = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        ParameterizedTypeImpl clsInfo = ParameterizedTypeImpl.make(((ParameterizedTypeImpl) type).getRawType(), new Type[]{cls}, null);
        this.type = clsInfo;
    }

    protected CustomTypeReference() {
        Type superClass = getClass().getGenericSuperclass();
        if (superClass instanceof Class<?>) {
            throw new IllegalArgumentException("Internal error: TypeReference constructed without actual type information");
        }
        this.type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
    }

    @Override
    public Type getType() {
        return type;
    }
}